package 进阶知识及API类库;

/**
 * Created By  醉美柳舞之众星捧月
 *
 * @author song
 * @date 2017/11/8 0:47
 */
public class 递归 {
    public static void main(String[] args) {
        // 递归  就是自己调用自己   比较危险的算法  很容易堆栈溢出 ; 干挂了虚拟机
        //  eg: 阶乘   4! = 4*3*2*1   3！=3*2*1   ===>   4!=4*3!   ====>   f(n) = n * f(n-1) ;
        System.out.println(factorial(10));
        System.out.println(fib(10));
    }

    public static long factorial(int n) {
        if (n == 0) {
            return 1L;
        } else {
            return n * factorial(n - 1);
        }
    }

    public static long fib(long index) {
        if (index == 0) {
            return 0;
        } else if (index == 1) {
            return 1;
        } else {
            return fib(index - 1) + fib(index - 2);
        }
    }
}
